home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / prospero / propsero.lha / prospero-beta.4.2e / user / vget.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  4KB  |  170 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  * Copyright (c) 1991, 1992 by the University of Southern California
  4.  *
  5.  * For copying and distribution information, please see the files
  6.  * <uw-copyright.h> and <usc-copyr.h>.
  7.  */
  8.  
  9. #include <uw-copyright.h>
  10. #include <usc-copyr.h>
  11. #include <sys/param.h>
  12. #include <sys/wait.h>
  13. #include <strings.h>
  14. #include <stdio.h>
  15.  
  16. #include <pfs.h>
  17. #include <psite.h>
  18. #include <perrno.h>
  19. #include <pcompat.h>
  20. #include <pmachine.h>
  21.  
  22. extern int    errno;
  23. int        perrno;
  24. int        pfs_debug = 0;
  25.  
  26.  
  27. main(argc, argv)
  28.     char *argv[];
  29.     {
  30.     char        am_args[MAX_PTXT_LEN];
  31.     char        path[MAX_VPATH];
  32.     char        vcachebin[MAXPATHLEN];
  33.     char        *lclfil;
  34.     char        *method;
  35.     int        am;
  36.     VLINK        vl;
  37.     int         pid;
  38.     union wait     status;
  39.  
  40.     int        message_option = 0;
  41.     int        avsflag = 0;  /* If set - use active VS (no closure) */
  42.     char        *debugflag = "-";
  43.     char        *verboseflag = "-v";
  44.  
  45.     argc--;argv++;
  46.  
  47.     while (argc > 0 && *argv[0] == '-' && *(argv[0]+1) != '\0') {
  48.         switch (*(argv[0]+1)) {
  49.         
  50.         case 'D':
  51.         pfs_debug = 1; /* Default debug level */
  52.         sscanf(argv[0],"-D%d",&pfs_debug);
  53.         debugflag = argv[0];
  54.         break;
  55.                 
  56.          case 'a':
  57.          avsflag++;
  58.          break;
  59.  
  60.         case 'm':
  61.         message_option++; 
  62.         break;
  63.         
  64.         case 'q':
  65.         verboseflag = "-";
  66.         break;
  67.  
  68.         case 'v':
  69.         verboseflag = "-v";
  70.         break;
  71.  
  72.         default:
  73.         fprintf(stderr,
  74.             "Usage: vget [-a,-m,-q,-v] virtual-file [local-file]\n");
  75.         exit(1);
  76.         }
  77.         argc--; argv++;
  78.     }
  79.  
  80.      /* The next argument must be the name of the file to retrieve */
  81.      /* within the virtual file system unless it is to extracted   */
  82.     /* from a mail message                                        */
  83.  
  84.     if(argc < 1 && !message_option) {
  85.         fprintf(stderr,"Usage: vget [-a,-m,-q,-v] virtual-file [local-file]\n");
  86.         exit(1);
  87.     }
  88.     
  89.     if (argc >= 1) strcpy(path,argv[0]);
  90.  
  91.     /* if stdin is not a tty and OK to use closure */
  92.     /* then we have to extract closure info        */
  93.     if(!avsflag && !isatty(0)) {
  94.         char    *s;
  95.         s = readheader(stdin,"virtual-system-name:");
  96.         if(!s)  {
  97.         fprintf(stderr,"vget: Can't find Virtual-System-Name.\n");
  98.         exit(1);
  99.         }
  100.         sprintf(path,"%s:",s);
  101.         
  102.         if(message_option) {
  103.         s = readheader(stdin,"virtual-file-name:");
  104.         if(!s) {
  105.             fprintf(stderr,"vget: Can't find Virtual-file-name.\n");
  106.             exit(1);
  107.         }
  108.         strcat(path,s);
  109.         }
  110.         else strcat(path,argv[0]);
  111.  
  112.     }
  113.     else if(message_option) {
  114.         fprintf(stderr,"vget: Can't find Virtual-file-name.\n");
  115.         exit(1);
  116.     }
  117.  
  118.     /* If second name was not specified, derive it from first */
  119.     if(argc == (message_option ? 0 : 1)) {
  120.         char *p;
  121.         p = rindex(path,'/');
  122.         lclfil = (p != NULL) ? p + 1 : path;
  123.     }
  124.     /* Otherwise local file is the second name */
  125.     else lclfil = (message_option ? argv[0] : argv[1]);
  126.  
  127.      vl = rd_vlink(path);
  128.  
  129.     if(!vl) {
  130.         if(perrno == PSUCCESS) fprintf(stderr,"vget: File not found\n");
  131.         else perrmesg("vget: ", 0, NULL);
  132.         exit(1);
  133.     }
  134.  
  135.      am = pget_am(vl,am_args,P_AM_AFTP | P_AM_FTP);
  136.  
  137.      if(am == P_AM_AFTP) method = "AFTP";
  138.     else if (am == P_AM_FTP) method = "FTP";
  139.     else {
  140.          fprintf(stderr,"vget: Can't access file using anonymous ftp\n");
  141.         exit(1);
  142.     }
  143.  
  144.     if(pwarn) pwarnmesg("WARNING: ",0,NULL);
  145.  
  146.     pid = fork();
  147.     if (pid == 0) {
  148. #ifdef USE_PATH
  149.         DISABLE_PFS(execlp("vcache","vcache",debugflag,verboseflag,
  150.                   vl->host,vl->filename,lclfil,
  151.                   method,am_args,0));
  152. #endif USE_PATH
  153.         sprintf(vcachebin,"%s/vcache",P_BINARIES);
  154.         DISABLE_PFS(execl(vcachebin,"vcache",debugflag,verboseflag,
  155.                   vl->host,vl->filename,lclfil,
  156.                   method,am_args,0));
  157.         fprintf(stderr,"vget: exec failed for %s (errno=%d)\n",vcachebin,errno);
  158.         exit(1);
  159.     }
  160.     else wait(&status);
  161.  
  162.     if(status.w_T.w_Retcode) {
  163.         fprintf(stderr,"vget: Retrieve failed\n");
  164.         exit(1);
  165.     }
  166.     
  167.     exit(0);
  168.  
  169.     }
  170.